<?php namespace web\admin\controller;
/** .-------------------------------------------------------------------
 * |  Software: [HDPHP framework]
 * |      Site: www.hdphp.com
 * |-------------------------------------------------------------------
 * |    Author: 向军 <2300071698@qq.com>
 * |    WeChat: aihoudun
 * | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
 * '-------------------------------------------------------------------*/

class Goodslist extends Common{
	private $db;
	public function __construct(){
		parent::__construct();
		$this->db=new \system\model\Goods_list;
	}
	//类型列表
	public function index() {
		View::with('title','货品管理列表');
		$gid=Q('get.gid',0,'intval');
		View::with('gid',$gid);
		$data=Db::table('goods_list')->where('gid',$gid)->get();
		foreach($data as $k=>$v){
			$data[$k]['combine']=explode('|',$v['combine']);
		}
//		p($data);die;
		foreach($data as $k=>$v){
			foreach($v['combine'] as $kk=>$vv){
				$data[$k]['combine'][$kk]=Db::table('goods_attr')->where('gtid',$vv)->pluck('gtvalue');
				
			}
		}
		View::with('data',$data);
		
//		获取规格数据

		$cmData=Db::table('type_attr')
		->join('goods_attr','type_attr.taid','=','goods_attr.taid')
		->where('type_attr.class',1)
		->where('goods_attr.gid',$gid)
		->groupBy('taid')
//		->orderBy('taid','ASC')
		->get(['type_attr.taid','type_attr.taname']);
		
//		p($cmData);
		foreach($cmData as $k=>$v){
			$cmData[$k]['old']=Db::table('goods_attr')->where('gid',$gid)->where('taid',$v['taid'])->orderBy('gtid','DESC')->get();
		}
		View::with('cmData',$cmData);
//		p($gid);
//		p($cmData);die;
		
		View::make('goods/goodlist.php');
	}
	
//	添加类型
	public function add(){
		View::with('title','添加类型');
		if(IS_POST){
//			p($_POST);die;
			$re=$this->db->store();
			if(!$re){
				message($this->db->getError(),'','error');die;
			}
//			添加成功
			message('添加成功');
		}
		
		View::make();
	}
	
//	检测数据是否存在
	public function dete(){
//		p($_POST);die;
		$com=implode('|',$_POST['combine']);
		$gid=Q('post.gid');
		$re=Db::table('goods_list')
		->where('gid',$gid)
		->where('combine',$com)
		->first();
//		p($re);die;
		if($re){
			message(0,'success');
		}
	}
	
//	编辑
	public function edit(){
		View::with('title','编辑货品');
		if(IS_POST){
			$gid=Q('post.gid',0,'intval');
//			p($_POST);die;
			$re=$this->db->edit();
			if(!$re){
				message($this->db->getError(),'','error');die;
			}
//			编辑成功
			message('编辑成功','','success');die;
		}
	}
//	获取编辑数据
	public function edData(){
		$glid=Q('post.glid',0,'intval');
		$edData=$this->db->where('glid',$glid)->first();
		$edData['combine']=explode('|',$edData['combine']);
				
		foreach($edData['combine'] as $k=>$v){
			$edData['comname'][]=Db::table('goods_attr')
			->join('type_attr','goods_attr.taid','=','type_attr.taid')
			->where('goods_attr.gtid',$v)
			->lists('taname,gtvalue');
		}
//		p($edData);die;
		message($edData,'','success');
	}
//	删除
	public function del(){
//		获取id
		$id=Q('get.glid',0,'intval');
		$gid=Q('get.gid',0,'intval');
		$re=$this->db->where('glid',$id)->delete();
		if(!$re){
			message('删除失败，请检测后重新删除',U('index'));
		}
		message('删除成功',U('index',array('gid'=>$gid)));
		
	}
	
	
	
}